package com.sduonline.isdapp.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class ImageUtil {

	/**
	 * 读取imgLink的图片并保存到savePath路径下。
	 * 
	 * @param imgLink
	 * @param savePath
	 * @return
	 */
	public static boolean savePicToLocal(String imgLink, String savePath, String imgName) {
		try {
			File path = new File(savePath);
			if (!path.exists()) {
				path.mkdirs();
			}

			Response resp = Jsoup.connect(imgLink).ignoreContentType(true).maxBodySize(1024 * 1024 * 5)
					.timeout(10 * 1000).execute();
			byte[] bytes = resp.bodyAsBytes();
			File file = new File(savePath + imgName);
			System.out.println("img saved in " + file.getAbsolutePath());

			OutputStream out = new FileOutputStream(file);

			out.write(bytes);
			out.flush();
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
